2020-2021 Sunseeker Telemetry and Lighting System
tlv_ex3_calibrateTempSensor.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
70 //******************************************************************************
71 
72 #include "driverlib.h"
73 
74 uint16_t temp;
75 volatile float DegF;
76 volatile float DegC;
77 float mref, nref;
78 uint8_t bADCCAL_bytes;
79 struct s_TLV_ADC_Cal_Data * pADCCAL;
80 
81 //******************************************************************************
82 //
83 // This function will initialize and calibrate ADC for temperature sensing.
84 // The embedded function call of Get_TLV_Info searches the TLV block of data
85 // (defined in the device header file) for the ADC calibration block, then
86 // returns the address of the first data descriptor to the pointer passed into
87 // it. The slope and offset of the corrected temperature curve are then
88 // calculated for temperature calibration purposes.
89 //
90 //******************************************************************************
91 void calibrateADC(void);
92 
93 void main (void)
94 {
95  //Stop Watchdog Timer
96  WDT_A_hold(WDT_A_BASE);
97 
98  calibrateADC();
99 
100  while (1)
101  {
102  //Enable/Start first sampling and conversion cycle
103  /*
104  * Base address of ADC12 Module
105  * Start the conversion into memory buffer 0
106  * Use the repeated sequence of channels
107  */
108  ADC12_B_startConversion(ADC12_B_BASE,
109  ADC12_B_MEMORY_0,
110  ADC12_B_SINGLECHANNEL);
111 
112  //LPM0 with interrupts enabled
113  __bis_SR_register(LPM4_bits + GIE);
114  __no_operation();
115 
116  //Temperature in Celsius
117  DegC = (temp - nref) / mref;
118 
119  //Temperature in Fahrenheit
120  DegF = (9*DegC)/5 + 32;
121 
122  //SET BREAKPOINT HERE and watch IntDegC and IntDegF
123  __no_operation();
124  }
125 }
126 
127 void calibrateADC(void)
128 {
129  //Initialize the ADC12B Module
130  /*
131  * Base address of ADC12B Module
132  * Use internal ADC12B bit as sample/hold signal to start conversion
133  * USE MODOSC 5MHZ Digital Oscillator as clock source
134  * Use default clock divider/pre-divider of 1
135  * Not use internal channel
136  */
137  ADC12_B_initParam initParam = {0};
138  initParam.sampleHoldSignalSourceSelect = ADC12_B_SAMPLEHOLDSOURCE_SC;
139  initParam.clockSourceSelect = ADC12_B_CLOCKSOURCE_ADC12OSC;
140  initParam.clockSourceDivider = ADC12_B_CLOCKDIVIDER_1;
141  initParam.clockSourcePredivider = ADC12_B_CLOCKPREDIVIDER__1;
142  initParam.internalChannelMap = ADC12_B_NOINTCH;
143  ADC12_B_init(ADC12_B_BASE, &initParam);
144 
145  //Enable the ADC12B module
146  ADC12_B_enable(ADC12_B_BASE);
147 
148  /*
149  * Base address of ADC12B Module
150  * For memory buffers 0-7 sample/hold for 64 clock cycles
151  * For memory buffers 8-15 sample/hold for 4 clock cycles (default)
152  * Disable Multiple Sampling
153  */
154  ADC12_B_setupSamplingTimer(ADC12_B_BASE,
155  ADC12_B_CYCLEHOLD_64_CYCLES,
156  ADC12_B_CYCLEHOLD_4_CYCLES,
157  ADC12_B_MULTIPLESAMPLESDISABLE);
158 
159  //Configure Memory Buffer
160  /*
161  * Base address of the ADC12B Module
162  * Configure memory buffer 0
163  * Map input Temperature Sensor to memory buffer 0
164  * Vref+ = VREF buffered
165  * Vref- = AVss
166  * Memory buffer 0 is not the end of a sequence
167  */
168  ADC12_B_configureMemoryParam configureMemoryParam = {0};
169  configureMemoryParam.memoryBufferControlIndex = ADC12_B_MEMORY_0;
170  configureMemoryParam.inputSourceSelect = ADC12_B_INPUT_TCMAP;
171  configureMemoryParam.refVoltageSourceSelect = ADC12_B_VREFPOS_INTBUF_VREFNEG_VSS;
172  configureMemoryParam.endOfSequence = ADC12_B_NOTENDOFSEQUENCE;
173  configureMemoryParam.windowComparatorSelect = ADC12_B_WINDOW_COMPARATOR_DISABLE;
174  configureMemoryParam.differentialModeSelect = ADC12_B_DIFFERENTIAL_MODE_DISABLE;
175  ADC12_B_configureMemory(ADC12_B_BASE, &configureMemoryParam);
176 
177  ADC12_B_clearInterrupt(ADC12_B_BASE,
178  0,
179  ADC12_B_IFG0
180  );
181 
182  //Enable memory buffer 0 interrupt
183  ADC12_B_enableInterrupt(ADC12_B_BASE,
184  ADC12_B_IE0,
185  0,
186  0);
187 
188  // If ref generator busy, WAIT
189  while(REF_A_BUSY == Ref_A_isRefGenBusy(REF_A_BASE));
190  // Select internal ref = 1.2V
191  Ref_A_setReferenceVoltage(REF_A_BASE, REF_A_VREF1_2V);
192  // Internal Reference ON
193  Ref_A_enableReferenceVoltage(REF_A_BASE);
194  // Wait for reference generator to settle
195  while(REF_A_READY != Ref_A_isVariableReferenceVoltageOutputReady(REF_A_BASE));
196 
197  // TLV access Function Call
198  TLV_getInfo(TLV_TAG_ADCCAL,
199  0,
200  &bADCCAL_bytes,
201  (uint16_t **)&pADCCAL
202  );
203 
204  mref = ((float)
205  (pADCCAL->adc_ref15_85_temp - pADCCAL->adc_ref15_30_temp))/(85-30);
206  nref = pADCCAL->adc_ref15_85_temp - mref*85;
207 }
208 
209 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
210 #pragma vector=ADC12_VECTOR
211 __interrupt
212 #elif defined(__GNUC__)
213 __attribute__((interrupt(ADC12_VECTOR)))
214 #endif
215 void ADC12ISR (void)
216 {
217  switch(__even_in_range(ADC12IV,12)) {
218  case 0: break; // Vector 0: No interrupt
219  case 2: break; // Vector 2: ADC12BMEMx Overflow
220  case 4: break; // Vector 4: Conversion time overflow
221  case 6: break; // Vector 6: ADC12BHI
222  case 8: break; // Vector 8: ADC12BLO
223  case 10: break; // Vector 10: ADC12BIN
224  case 12: // Vector 12: ADC12BMEM0 Interrupt
225  //Move results, IFG is cleared
226  temp = ADC12_B_getResults(ADC12_B_BASE, ADC12_B_MEMORY_0);
227 
228  //Exit active CPU
229  __bic_SR_register_on_exit(LPM4_bits);
230  break;
231  case 14: break; // Vector 14: ADC12BMEM1
232  case 16: break; // Vector 16: ADC12BMEM2
233  case 18: break; // Vector 18: ADC12BMEM3
234  case 20: break; // Vector 20: ADC12BMEM4
235  case 22: break; // Vector 22: ADC12BMEM5
236  case 24: break; // Vector 24: ADC12BMEM6
237  case 26: break; // Vector 26: ADC12BMEM7
238  case 28: break; // Vector 28: ADC12BMEM8
239  case 30: break; // Vector 30: ADC12BMEM9
240  case 32: break; // Vector 32: ADC12BMEM10
241  case 34: break; // Vector 34: ADC12BMEM11
242  case 36: break; // Vector 36: ADC12BMEM12
243  case 38: break; // Vector 38: ADC12BMEM13
244  case 40: break; // Vector 40: ADC12BMEM14
245  case 42: break; // Vector 42: ADC12BMEM15
246  case 44: break; // Vector 44: ADC12BMEM16
247  case 46: break; // Vector 46: ADC12BMEM17
248  case 48: break; // Vector 48: ADC12BMEM18
249  case 50: break; // Vector 50: ADC12BMEM19
250  case 52: break; // Vector 52: ADC12BMEM20
251  case 54: break; // Vector 54: ADC12BMEM21
252  case 56: break; // Vector 56: ADC12BMEM22
253  case 58: break; // Vector 58: ADC12BMEM23
254  case 60: break; // Vector 60: ADC12BMEM24
255  case 62: break; // Vector 62: ADC12BMEM25
256  case 64: break; // Vector 64: ADC12BMEM26
257  case 66: break; // Vector 66: ADC12BMEM27
258  case 68: break; // Vector 68: ADC12BMEM28
259  case 70: break; // Vector 70: ADC12BMEM29
260  case 72: break; // Vector 72: ADC12BMEM30
261  case 74: break; // Vector 74: ADC12BMEM31
262  case 76: break; // Vector 76: ADC12BRDY
263  default: break;
264  }
265 }
void calibrateADC(void)
volatile float DegF
uint16_t temp
void main(void)
uint8_t bADCCAL_bytes
struct s_TLV_ADC_Cal_Data * pADCCAL
void ADC12ISR(void)
volatile float DegC
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)